# -*- encoding: utf-8 -*-
##############################################################################
#
##############################################################################

from odoo import api, fields, models, _
from odoo.addons import decimal_precision as dp
from odoo.exceptions import UserError, ValidationError
from odoo.tools import float_round

class product_template(models.Model):
    _inherit = 'product.template'

    product_description=fields.Html(u"产品说明")
    product_attention=fields.Html(u"注意事项")
    product_production_attribute_ids = fields.One2many("product.production.attribute.line", "product_template_id",
                                                       string=u"加工需求")

    @api.multi
    def create_variant_ids(self):
        res = super(product_template, self).create_variant_ids()
        for tmpl_id in self.with_context(active_test=False):
            vals = {
                "product_production_attribute_ids": [(0, 0, {
                    "attribute_id": line.attribute_id.id,
                    "value_ids": [(6, 0, line.value_ids.ids)]
                }) for line in tmpl_id.product_production_attribute_ids]
            }
            for product in tmpl_id.product_variant_ids:
                if not product.product_production_attribute_ids:
                    product.write(vals)
        return res

    @api.multi
    def update_variant_attention(self):
        for template in self:
            if template.product_production_attribute_ids:
                for variant in template.product_variant_ids:
                    variant.product_production_attribute_ids.unlink()
                    variant.product_production_attribute_ids = [(0, 0, {
                        "attribute_id": l.attribute_id.id,
                        "value_ids": [(6, 0, l.value_ids.ids)]
                    }) for l in template.product_production_attribute_ids]
        return True


class ProductProduct(models.Model):
    _inherit = "product.product"
    product_production_attribute_ids = fields.One2many("product.production.attribute.line", "product_id",
                                                       string="加工需求")


class product_attention(models.Model):
    _name = 'product.attention'

    name = fields.Text(u"条件名称")
    product_condition = fields.Text(u"内容")
    product_template_id=fields.Many2one('product.template',u"产品")

